<?php 
    include "database.php";
    
    //Za delovanje seje na vseh nivojih
    session_set_cookie_params(0,"/");
    session_start();
    
    // Ce smo editirali lastne podatke
    if(isset($_GET["username"])){
    	$username = $_GET["username"];
    	$_SESSION["username"] = $username;
    }
    if(!isset($_SESSION['username'])) {
        header("Location: login.php");
    } 
    
    $admin = checkAdmin($_SESSION['id']);
    $usr_id_cp = $_SESSION['id'];
    
    //Del, ki inicializira spremenljivko msg_id, za izpis sporocila
    //$msg_id=-1; //Ne obstaja
    //if(isset($_GET['msg']))
     //   $msg_id=$_GET['msg'];
?>
<!DOCTYPE html>

<html>
	<head>
	    <meta charset="utf-8">
		
		<!-- Font weight: light (300) & semibold (600) -->
		<link href='http://fonts.googleapis.com/css?family=Open+Sans:600,300' rel='stylesheet' type='text/css'>
		<link rel="stylesheet" href="css/default.css"/>
		<link rel="stylesheet" href="css/tabs.css"/>
		<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.2/themes/smoothness/jquery-ui.css" />
		
		<script type="text/javascript" src="js/jquery-1.9.1.min.js"></script>
		<script type="text/javascript" src="js/jquery-ui-1.10.2.custom.min.js"></script>
		<script type="text/javascript" src="js/actions.js"></script>
		
		<title>Scrum | Control Panel</title>		
	</head>
	
	<body>
                <input type="hidden" id="usr_id_global" name="usr_id_name" value=<?= $usr_id_cp?> />
		<span id="username"><?php echo $_SESSION["username"];  ?></span>
		<span id="separator">|</span>
		<span id="role"><a href="edit_profile.php">edit</a></span>
		
		<h1>Scrum</h1>
                <?php showMessage();?>
		<menu>
			<li><a class="active" href="control_panel.php">control<span>Panel</span></a>&nbsp;&nbsp;|&nbsp;</li>
			<li><a href="project_wall.php">project<span>Wall</span></a>&nbsp;&nbsp;|&nbsp;</li>
			<li><a href="product_backlog.php">product<span>Backlog</span></a>&nbsp;&nbsp;|&nbsp;</li>
			<li><a href="sprint_backlog.php">sprint<span>Backlog</span></a>&nbsp;&nbsp;|&nbsp;</li>
			<li><a href="my_tasks.php">my<span>Tasks</span></a>&nbsp;&nbsp;|&nbsp;</li>
			<li><a href="progress_report.php">progress<span>Reports</span></a>&nbsp;&nbsp;|&nbsp;</li>
			<li><a href="./php/logout.php">logout</a></li>
		</menu>
		
		<section>
		
			<ul id="tabs">
                            
				<?php if($admin == 1){ //Preverjamo, če imamo uporabniške pravice... ?>
                                    <li><h2><a href="#" name="all_users" class="refresh">all<span>Users</span></a></h2></li>
                                    <li><h2><a href="#" name="add_user">add<span>User</span></a></h2></li>
                                <?php }?>
                                    <li><h2><a href="#" name="al_proj">all<span>Projects</span></a></h2></li>
				<?php if($admin == 1){?>
                                    <li><h2><a href="#" name="add_proj">add<span>Project</span></a></h2></li>
                                <?php }?>
			</ul>
			
			<div id="tab_content">
                                <?php if($admin == 1){ //Preverjamo, če imamo uporabniške pravice... ?>
				<!--===========-->
				<!-- All users -->
				<!--===========-->
			
				<div id="all_users" class="bracket bracket_height">
					
				<?php 
					$query = "SELECT * FROM user;" ;
					$queryResult = executeQuery($query);
					while ($row = mysql_fetch_array($queryResult)) {
						$user_id = $row['id'];
						$username = $row[1];
						$name = $row[2];
						$last_name = $row[3];
						$mail = $row[4];
						
						 ?>
						<div id="a" class="project">
                                                        <input type="hidden" name="user_id" value=<?= $user_id ?> />
							<div class="right">|&nbsp;<a href="#" class="edit">edit</a>&nbsp;•&nbsp;<a href="#" class="delete">delete</a></div>
							<div class="left"><h4> <?= $username ?> </h4>, <?= $mail ?> </div>
					    </div>
					
						<div class="project_editing">
						<p id="error_msg" class="alert_string"></p>
						<form method="post" class="editing_form" >
							<input type="hidden" name="user_id" value=<?= $user_id ?> /> <br/>
							<input type="text" name="username" placeholder="<?= $username ?>" value="<?= $username ?>" required="required" /> <br/>
							<input type="text" name="name" placeholder="<?= $name ?>" value="<?= $name ?>" required="required" /> <br/>
							<input type="text" name="last_name" placeholder="<?= $last_name ?>" value="<?= $last_name ?>" required="required" /> <br/>
							<input type="email" name="mail" placeholder="<?= $mail ?>" value="<?= $mail ?>" required="required" /> <br/>
							<div class="styled_select editing_select">
								<select name="admin"> 
									<option value="" disabled>role</option>
						<?php if($row[6] == 1){ ?>
									<option value=1 selected>administrator</option>
									<option value=0 >normal user</option>
						<?php } else { ?>
									<option value=1 >administrator</option>
									<option value=0 selected>normal user</option>
						<?php } ?>
								</select>
							</div>
							<input type="password" name="password" placeholder="password" required="required" />
							<input type="password" name="password_confirm" placeholder="confirm password" required="required" /> <br/>
							<button type="submit" class="edit_user">confirm</button>
							<button type="button" class="right_button">cancel</button>
						</form>
						</div>
						<div class="project_deleting">
							<button type="button" class="delete_user">delete</button>
							<button type="button" class="right_button">cancel</button>
						</div>
						
					<?php } ?>
				
			
				</div>
				
				<!--==========-->
				<!-- Add user -->
				<!--==========-->
				
				<div id="add_user" class="bracket">
                                    <p class="alert_string hidden">This username already exists!</p>
                                    <form id="add_user_form" method="post">
                                            <input type="text" name="username" placeholder="username" required="required" /> <br/>
                                            <input type="text" name="first_name" placeholder="first name" required="required" /> <br/>
                                            <input type="text" name="last_name" placeholder="last name" required="required" /> <br/>
                                            <input type="email" name="email" placeholder="e-mail" required="required" /> <br/>
                                            <div class="styled_select">
                                                    <select name="role">
                                                            <option value="" disabled selected>role</option>
                                                            <option value="1">administrator</option>
                                                            <option value="0">normal user</option>
                                                    </select>
                                            </div>
                                            <input type="password" name="password" placeholder="password" required="required" />
                                            <input type="password" name="password_confirm" placeholder="confirm password" required="required" /> <br/>
                                            <input id="add_user_submit" type="submit" name="submit" value="register" formaction="./php/addUser.php" />
                                    </form>
				</div>
				
				<!--==============-->
				<!-- All projects -->
				<!--==============-->
				<?php } ?>
				<div id="al_proj" class="bracket bracket_height">
                                        <!-- !!!!!!!!!!!!!!!--==============-->
                                        <?php
                                            
                                            $u_id = $_SESSION['id'];
                                            //Pogledamo še kateri projekt ima trenutni uporabnik izbran
                                            $query = "SELECT current_project FROM user WHERE id = '$u_id'";
                                            $selectedProjectID = mysql_fetch_array(executeQuery($query))[0];
                                            //$selectedProjectID = $row[0];
                                            
                                            //Če je admin mu prikažemo vse projekte
                                            if($admin == 1){
                                                $query = "SELECT * from project";
                                            }
                                            // V nasprotnem primeru prikažemo samo projekte pri katerih sodelujemo
                                            else{
                                                $query = "SELECT * FROM project WHERE project_id IN (SELECT project_id FROM project_user WHERE user_id = $u_id) OR product_owner = $u_id OR scrum_master = $u_id";
                                            }
                                            
                                            
                                            $result = executeQuery($query);
                                            while($row = mysql_fetch_array($result)){
                                                $id = $row['project_id'];
                                                $name = $row['project_name'];
                                                $desc = $row['description'];
                                                $lead_id = $row['product_owner'];
                                                $scrum_id = $row['scrum_master'];
                                                
                                                $query2 = "SELECT username FROM user WHERE id = '$lead_id'";
                                                $row = mysql_fetch_array(executeQuery($query2));
                                                $leader = $row['username'];
                                                
                                                $query2 = "SELECT username FROM user WHERE id = '$scrum_id'";
                                                $row = mysql_fetch_array(executeQuery($query2));
                                                $scrum_master = $row['username'];
                                                
                                                
                                        ?>
					<div class="project">
						<input type="hidden" name="project_id" value="<?= $id ?>" />
                                            <?php 
                                                if($selectedProjectID == $id){
                                                    $choosenProject = "<a href='#' class='select_proj active'>selected</a>";
                                                }
                                                else{
                                                    $choosenProject = "<a href='#' class='select_proj'>select</a>";
                                                }
                                                if($admin == 1 || $scrum_id == $u_id){
                                                    $disableEdit = "";
                                                    ?>
                                                    <div class="right">|&nbsp;<?= $choosenProject ?>&nbsp;•&nbsp;<a href="#" class="edit">edit</a>&nbsp;•&nbsp;<a href="#" class="delete">delete</a></div>
                                            <?php } else{
                                                $disableEdit = "disabled";
                                                ?>
                                                  <div class="right">|&nbsp;<?= $choosenProject ?>&nbsp;•&nbsp;<a href="#" class="edit">view</a></div>  
                                            <?php }?>
                                                <div class="left"><h4><?= $name?></h4>, <?= $leader?></div>
					</div>
					<div class="project_editing">
                                            
                                                <p class="alert_string hidden">This project name already exists!</p>
                                                
						<form method="post" class="editing_form" action="./php/editProject.php">
                                                        <input type="hidden" name="project_id" value="<?= $id ?>" />
							<input <?=$disableEdit ?> type="text" name="name" placeholder="project name" value="<?= $name?>" required="required" /> <br/>
							<textarea <?=$disableEdit ?> name="description" placeholder="description..." required="required" ><?= $desc?></textarea>
							<div class="styled_select editing_select">
								<select <?=$disableEdit ?> name="leader">
									<option value="" disabled>product owner</option>
									<?php dropDownUsers($lead_id) ?>
								</select>
							</div>
							<div class="styled_select editing_select">
								<select <?=$disableEdit ?> name="master">
									<option value="" disabled>scrum master</option>
									<?php dropDownUsers($scrum_id) ?>
								</select>
							</div>
							<div class="checkbox_container edit_checkbox">
								<?php
                                                                    //Izbira uporabnikov na projektu
                                                                    $query = "SELECT user_id FROM project_user WHERE project_id='$id'";
                                                                    $res = executeQuery($query);
                                                                    $user_ids = array(); //Tabela vseh uporabnikov, ki sodelujejo na projektu
                                                                    while($row = mysql_fetch_array($res)){
                                                                        $user_ids[] = $row['user_id'];
                                                                    }
                                                                    //Preberemo še vse uporabnike
                                                                    $query = "SELECT id,username FROM user;" ;
                                                                    $queryResult = executeQuery($query); 
                                                                    $boolDisabled = false;
                                                                    
                                                                    
                                                                    if($disableEdit == "disabled"){
                                                                        $boolDisabled = true;
                                                                        $checked = ""; //To check checkbox
                                                                    }
                                                                    while ($row = mysql_fetch_array($queryResult)) {
                                                                        (in_array($row[0], $user_ids)) ? $check="checked" : $check="";
                                                                        if($row[0]==$scrum_id){
                                                                            $checked = "checked";
                                                                            if(!$boolDisabled)
                                                                                $disableEdit="disabled";
                                                                        }
                                                                        echo "<label><input $disableEdit $checked type='checkbox' name='users[]' value='$row[0]' $check>" .$row[1]."</label><br/>" ;
                                                                        if(!$boolDisabled){
                                                                            $disableEdit="";
                                                                            $checked="";
                                                                        }
                                                                    }
                                                                ?>
							</div>
                                                    <?php if($admin == 1 || $scrum_id == $u_id){ ?>
                                                        <button type="submit" class="edit_project">confirm</button>
                                                    <?php } ?>
							<button type="button" class="right_button">cancel</button>
						</form>
					</div>
					<div class="project_deleting">
                                                <button type="button" class="delete_project">delete</button>
						<button type="button" class="right_button">cancel</button>
					</div>
					<?php }?>
					
				</div>
				
				<!--=============-->
				<!-- Add project -->
				<!--=============-->
				<?php if($admin == 1){ //Preverjamo, če imamo uporabniške pravice... ?>
				<div id="add_proj" class="bracket">
					
					<p id="error_msg2" class="alert_string" ></p>
					<form id="add_project_form" method="post">
						<input type="text" name="project_name" placeholder="project name" required="required" /><br/>
						<textarea name="description" value="" placeholder="description..."></textarea>
						<div class="styled_select">			
							<select name="leader">
								<option value="" disabled selected>product owner</option>

							<?php 
								$query = "SELECT id,username FROM user;" ;
								$queryResult = executeQuery($query);
								while ($row = mysql_fetch_array($queryResult)) {
									echo "<option value=\"$row[0]\">".$row[1]."</option>" ;
								}
							?>	
							</select>
						</div>
						<div class="styled_select">
							<select name="master">
								<option value="" disabled selected>scrum master</option>

							<?php
								$query = "SELECT id,username FROM user;" ;
								$queryResult = executeQuery($query); 
								while ($row = mysql_fetch_array($queryResult)) {
									echo "<option value=\"$row[0]\">".$row[1]."</option>" ;
								}
							?>
							
							</select>
						</div>
						<div class="checkbox_container">
						
							<?php
								$query = "SELECT id,username FROM user;" ;
								$queryResult = executeQuery($query); 							
								while ($row = mysql_fetch_array($queryResult)) {
									echo "<label><input type=\"checkbox\" name=\"value[]\" value=\"$row[0]\">" .$row[1]."</label><br/>" ;
								}
							?>
						</div>
						<input id="add_project_submit" type="submit" name="submit" value="create" formaction="./php/addProject.php" />
					</form>
				</div>
                                <?php } ?>
			</div>
		
		</section>
		
	</body>
</html>